//
//  RJDetailTable.m
//  AirQuality
//
//  Created by Po on 15/5/12.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJDetailTable.h"

@interface RJDetailTable () <UITableViewDataSource, UITableViewDelegate>

@property (retain, nonatomic) NSArray * data;

@end

@implementation RJDetailTable

- (instancetype)initWithData:(NSArray *)data
{
    self = [super initWithFrame:ScreenBounds];
    if (self) {
        UIView * view = [self getNibView];
        if (DeviceType < 6) {
            [view setFrame:CGRectMake(40, 100, ScreenWidth - 80, ScreenHeight - 200)];
        } else {
            [view setFrame:CGRectMake(40, 150, ScreenWidth - 80, ScreenHeight - 300)];
        }
        [self addSubview:view];
        [self setOptionWithView:view];
        [self.tableView setDelegate:self];
        [self.tableView setDataSource:self];
        [self initData:data];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)data
{
    self = [super initWithFrame:ScreenBounds];
    if (self) {
        UIView * view = [self getNibView];
        [view setFrame:frame];
        [self setOptionWithView:view];
        [self addSubview:view];
        [self initData:data];
    }
    return self;
}

- (void)initData:(NSArray *)data
{
    _isShowSection = YES;
    _data = [NSArray arrayWithArray:data];
}

/**设置相关属性*/
- (void)setOptionWithView:(UIView *)view
{
    [view.layer setShadowOffset:CGSizeMake(2, 2)];
    [view.layer setShadowOpacity:0.8];
    [view.layer setCornerRadius:5];
}

- (UIView *)getNibView
{
    NSArray * nibArray = [[NSBundle mainBundle] loadNibNamed:@"RJDetailTable" owner:self options:nil];
    if (nibArray.count == 0) {
        [RJTools showMsg:@"程序文件缺失，请重新安装" time:3];
        return [[UIView alloc] init];
    }
    return nibArray[0];
}

- (IBAction)pressCancelButton:(UIButton *)sender {
    [self removeFromSuperview];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!_isShowSection) {
        return 1;
    }
    return _data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary * dic = _data[section];
    NSArray * array = [dic allValues][0];
    return array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (!_isShowSection) {
        return 0;
    }
    return 44.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!_isShowSection) {
        return [[UIView alloc] init];
    }
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 44)];
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:view.bounds];
    [titleLabel setBackgroundColor:[UIColor grayColor]];
    [titleLabel setText:[_data[section] allKeys][0]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel.layer setShadowOffset:CGSizeMake(2, 2)];
    [titleLabel.layer setShadowOpacity:0.8];
    [view addSubview:titleLabel];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdent = @"DetailTableCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
        [cell.contentView setBackgroundColor:[UIColor clearColor]];
        [cell.selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
    }
    
    NSString * titleString;
    if ([RJUserData defaultRJUserData].isProvince) {
        NSArray *data = [_data[indexPath.section] allValues][0];
        titleString = data[indexPath.row];
    } else {
        titleString = _data[indexPath.row];
    }
    [cell.textLabel setText:titleString];

    
    return cell;
}

#pragma mark - system
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}
@end
